// --------------------------------------------------------------------------------------------------------------------
// <copyright file="RobotiqSModelAgent.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.Robotics.Hardware.Runtime.Robotiq
{
    using System;
    using System.Net.Sockets;
    using System.Runtime.Serialization;
    using Microsoft.Robotics.Hardware.Robotiq;
    using Microsoft.Robotics.Numerics;
    using Microsoft.Robotics.Runtime;

    /// <summary>
    /// Controls and publishes state for Robotiq S Model gripper
    /// </summary>
    [System.Diagnostics.CodeAnalysis.SuppressMessage(
        "Microsoft.Design",
        "CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable",
        Justification = "Agent handles disposal of unmanaged resources in OnDeactivate()")]
   [DataContract]
    public class RobotiqSModelAgent :
        ConsumerProducerAgent<TimerMessage, RobotiqSModelStateMessage>,
        IControlReceiver<RobotiqSModelControlMessage>
    {
        /// <summary>
        /// Gripper takes approx 20s to initialize
        /// </summary>
        private const int GripperResetTimeInMS = 20000;

        /// <summary>
        /// Object for interfacing with the gripper
        /// </summary>
        private RobotiqSModelGripper gripper;

        /// <summary>
        /// Initializes a new instance of the RobotiqSModelAgent class
        /// </summary>
        /// <param name="name">Agent name</param>
        /// <param name="producer">Producer to subscribe to</param>
        public RobotiqSModelAgent(string name, IProducer<TimerMessage> producer)
            : base(name, producer.Name)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="RobotiqSModelAgent"/> class
        /// </summary>
        /// <param name="name">Agent name</param>
        /// <param name="producer">Producer to subscribe to</param>
        /// <param name="gripperIPAddress">Ip address of the griper. </param>
        public RobotiqSModelAgent(
            string name,
            IProducer<TimerMessage> producer,
            string gripperIPAddress)
            : this(name, producer)
        {
        }

        /// <summary>
        /// Initializes the agent.
        /// </summary>
        /// <param name="locator">The agent locator.</param>
        public override void Initialize(AgentLocator locator)
        {
            base.Initialize(locator);
            this.gripper = new RobotiqSModelGripper();
        }

        /// <summary>
        /// Called when the agent receives a control message.
        /// </summary>
        /// <param name="message">The received message.</param>
        public void ReceiveControl(RobotiqSModelControlMessage message)
        {
            if (message.Reset)
            {
                this.gripper.ResetGripper(message.Command.Activate, GripperResetTimeInMS);
            }
            else
            {
                this.gripper.SetBasicCommand(message.Command);
            }
        }

        /// <summary>
        /// Called when the agent receives a message.
        /// </summary>
        /// <param name="message">The received message.</param>
        public override void Receive(TimerMessage message)
        {
            this.gripper.UpdateState();

            RobotiqSModelStateMessage stateMessage = new RobotiqSModelStateMessage(message, this.gripper.State);

            this.Publisher.Post(stateMessage);
        }

        /// <summary>
        /// Called when agent is deactivated.  Dispose of unmanaged resources.
        /// </summary>
        public override void OnDeactivated()
        {
            base.OnDeactivated();
            if (this.gripper != null)
            {
                this.gripper.Dispose();
            }
        }
    }
}
